package com;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.net.URL;

@SpringBootApplication
@MapperScan(basePackages = {"com.dao"})
public class yiyaoguanlixitongApplication extends SpringBootServletInitializer implements ApplicationListener<ContextRefreshedEvent> {

    @Value("${server.port:8080}")
    private int serverPort;

    @Value("${server.servlet.context-path:/}")
    private String contextPath;

    public static void main(String[] args) {
        SpringApplication.run(yiyaoguanlixitongApplication.class, args);
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        try {
            URL front = yiyaoguanlixitongApplication.class.getClassLoader().getResource("front/front/index.html");
            URL front2 = yiyaoguanlixitongApplication.class.getClassLoader().getResource("front/front/dist/index.html");
            URL admin = yiyaoguanlixitongApplication.class.getClassLoader().getResource("admin/admin/dist/index.html");
            if (front != null) {
                String frontUrl = "http://localhost:" + serverPort + contextPath + "/front/index.html";
                Runtime.getRuntime().exec("cmd /c start " + frontUrl);
            } else if (front2 != null) {
                String front2Url = "http://localhost:" + serverPort + contextPath + "/front/dist/index.html";
                Runtime.getRuntime().exec("cmd /c start " + front2Url);
            } else if (admin != null) {
                String adminUrl = "http://localhost:" + serverPort + contextPath + "/admin/dist/index.html";
                Runtime.getRuntime().exec("cmd /c start " + adminUrl);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) {
        return applicationBuilder.sources(yiyaoguanlixitongApplication.class);
    }

}
